<!DOCTYPE html>
<html lang="ch">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/logout.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="javascript:logout()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div id="ArticesDiv" class="container-right" style="margin-left: 10%;">
        
        </div>
    </div>
    <script>
        function myArticles() {
            jQuery.ajax({
                url:"/article/list",
                type:"get",
                data:{},
                success: function(res) {
                    if (res.code == 200) {
                        // 请求成功
                        var createHtml = ""; // 拼接页面
                        var articles = res.data;
                        if (articles == null || articles.length == 0) {
                            // 新用户没有文章
                            createHtml += "<h3 style='margin-left:20px;margin-top:20px'>暂无文章，请先"+
                                "<a href='blog_add.html'>添加</a>！</h3>";
                        } else {
                            // 发布过文章的用户
                            for (var i = 0; i < articles.length; i++) {
                                var article = articles[i]; 
                                createHtml += '<div class="blog">';
                                createHtml += '<div class="title">'+article.title+'</div>';
                                createHtml += '<div class="date">'+article.createTime+'</div>';
                                createHtml += '<div class="desc">'+article.content+'</div>';
                                createHtml += '<a href="blog_content.html?aid='+article.id+'" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;'
                                createHtml += '<a href="blog_edit.html?aid='+article.id+'" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;'
                                createHtml += '<a href="javascript:del('+article.id+')" class="detail">删除 &gt;&gt;</a>';
                                createHtml += '</div>';
                            }
                        }
                        jQuery("#ArticesDiv").html(createHtml);
                    } else {
                        // 请求失败
                        alert("操作失败:"+res.msg);
                        location.href = "login.html";
                    }
                }
            });
        }
        myArticles();

        function del(aid) {
            // 参数校验
            if (aid == "" || aid <= 0) {
                alert("参数错误");
                return false;
            }
            // 将参数返回给后端
            if (confirm("是否删除文章？")) {
                jQuery.ajax({
                    url:"/user/delete",
                    type:"post",
                    data:{
                    "aid":aid
                    },
                    success: function(res) {
                        if (res.code == 200 && res.data == 1) {
                            location.href = location.href;
                        } else {
                            alert("操作失败："+res.msg);
                        }
                    }
                });
            }
        }
    </script>
</body>

</html>